package bid

import (
	"golang.org/x/time/rate"
	"sync"
	"wx-api/adx-bid/cache"
	"wx-api/adx-bid/model/m_channellist"
)

type BudgetReq struct {
	Type      string //协议类型
	Code      string //标示
	Url       string //请求url
	Method    string //请求方式：get/post
	IsQps     bool   //是否进行qps限制
	QpsLimit  int
	RateLime  *rate.Limiter
	Timems    int //超时时间
	HeaderMap map[string]string
	IsZip     bool //是否压缩
	IsJson    bool //是否json发送
}

var BudgetMapArr = new(sync.Map)

func GetBudgetReq(id string) *BudgetReq {
	tmp, ok := BudgetMapArr.Load(id)
	if !ok {
		d_info, ok := cache.CacheChannelListMapGet(id)
		if !ok {
			return nil
		}
		return InitBudgetReq(*d_info)
	}
	return tmp.(*BudgetReq)
}

// 上游请求初始化
func InitBudgetMap() {
	budget_all := cache.CacheChannelListMapAll()
	for _, list := range budget_all {
		InitBudgetReq(list)
	}
}

func InitBudgetReq(list m_channellist.ChannelList) *BudgetReq {
	qps := 1000
	tmp_req := &BudgetReq{
		IsQps:    false,
		QpsLimit: qps,
		RateLime: rate.NewLimiter(rate.Limit(float64(qps)), qps),
	}
	switch list.ApiType {
	case "ad360":
	}
	BudgetMapArr.Store(list.Uqid, tmp_req)
	return tmp_req
}
